package com.leyou.item.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 张伟奇
 * @Date 2021/7/18 9:59
 * @description
 * @Version 1.0
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {
   @Autowired
   private SpuService spuService;
   @Autowired
   private SkuService skuService;
   @Autowired
   private SpuDetailService spuDetailService;


    /**
     * spu的分页查询
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>>  listSpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "saleable" ,required = false) Boolean saleable
    ){

        PageDTO<SpuDTO> spuDTOPageDTO = spuService.listSpuByPage(page, rows, saleable, categoryId, brandId, id);

        return ResponseEntity.ok(spuDTOPageDTO);
    }

    /**
     * 根据ids查询sku
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIds(@RequestParam("ids") List ids){

        return ResponseEntity.ok(this.skuService.listSkuByIds(ids));
    }

    /**
     * 根据id查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpuId(@RequestParam("id") Long id){

        return  ResponseEntity.ok(this.skuService.listSkuBySpuId(id));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<List<SpuDetailDTO>> listSpuDetailById(@RequestParam("id") Long id){

        return ResponseEntity.ok(this.spuDetailService.listSpuDetailById(id));
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */

    @GetMapping("/spu/{id}")
    public ResponseEntity<List<SpuDTO>> listSpuById(@PathVariable("id") Long id){
        return ResponseEntity.ok(this.spuService.listSpuById(id));
    }

    @GetMapping("/{id}")
    public ResponseEntity<List<SpuDTO>>  listSpuWithSkuWithSpuDetail(@PathVariable("id") Long id){
        return ResponseEntity.ok(this.spuService.listSpuWithSkuWithSpuDetail(id));
    }

    /**
     * 商品的新增包含spu，sku,spuDetail
     * @param spuDTO
     * @return
     */

    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(@RequestBody SpuDTO spuDTO){
        this.spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 商品的上下架
     * @param spuId
     * @param saleable
     * @return
     */

    @PutMapping("/saleable")
    public ResponseEntity<Void> updateGoodssaleable(
            @RequestParam("id") Long spuId,
            @RequestParam("saleable") Boolean saleable
    ){

        this.spuService.updateGoodssaleable(spuId,saleable);
        return ResponseEntity.ok().build();
    }

    /**
     * 商品的修改   可能包含spu，sku,spuDetail
     * @param spuDTO
     * @return
     */

    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        this.spuService.updateGoods(spuDTO);
        return ResponseEntity.ok().build();
    }


}
















